//
//  SaTMNewWatermarkPhotoVc.m
//  saas
//
//  Created by apple on 2020/11/24.
//  Copyright © 2020 fangmao. All rights reserved.
//

#import "SaTMNewWatermarkPhotoVc.h"
#import "SaTMNewPublicPhotoCollectionViewCell.h"
#import "SDWebImageManager.h"
#import "SaImageZoomView.h"


@interface SaTMNewWatermarkPhotoVc ()<UICollectionViewDelegate,UICollectionViewDataSource,protocolToachPublicPhotoViewDelegate>{
    NSInteger _idx;
}

@property (nonatomic, strong) UIImage *displayImage;
@property (nonatomic,copy) NSString * imgStr;
@property (nonatomic, strong) SaImageZoomView *imgView;


@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong)SaTMNewManagerBJianModel * model;
@property (nonatomic,copy) NSString * waterTextStr;
@property (nonatomic,assign) BOOL Istype;



@property (nonatomic,strong) NSMutableArray * filesArr;
@property (nonatomic,strong) NSMutableArray * loadArr;
@property (nonatomic,strong) NSMutableArray * NetArr;
@property (nonatomic,copy) NSString * type;

@end

@implementation SaTMNewWatermarkPhotoVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _idx = 1;
    self.title = self.titleStr;
    [self config];
 
}


-(void)addRightNavigationUI{
    
    UIImage * image = [UIImage imageNamed:@"icon_shanchu1"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(deletePhoto)];
    
    
}


-(void)setDeleteBool:(BOOL)deleteBool{
    _deleteBool = deleteBool;
    if (_deleteBool) {
        [self addRightNavigationUI];
    }
}


-(void)reloadImgWithFilesArray:(NSArray *)files WaterText:(NSString *)waterText Type:(BOOL)type{
    self.dataSource = [NSMutableArray arrayWithArray:files];
    self.waterTextStr = waterText;
    self.Istype = type;
    [self.collectionView reloadData];
}

-(void)config{

    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
}

#pragma mark -- 点击事件

-(void)deletePhoto{
    
    UIAlertController * sheet = [UIAlertController alertControllerWithTitle:@"" message:@"要删除这张图片吗？" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction * action = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
            __weak typeof(self)weakSelf = self;
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(deletePhoto:)]) {
                [weakSelf.delegate deletePhoto:self.dataSource[self->_idx - 1]];
            }
            [self.dataSource removeObjectAtIndex:self->_idx - 1];
            self.title = [NSString stringWithFormat:@"%@/%@",@(self->_idx),@(self.dataSource.count)];
            [self.collectionView reloadData];
            if (self.dataSource.count == 0) {
                [self.navigationController popViewControllerAnimated:YES];
            }
    }];
    [sheet addAction:action];
    
    [action setValue:[UIColor redColor] forKey:@"titleTextColor"];
    
    UIAlertAction * canleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [sheet addAction:canleAction];
    
    [self presentViewController:sheet animated:YES completion:nil];
    
}



#pragma mark -- protocolToachPublicPhotoViewDelegate
-(void)hiddenOrShowNavigation:(BOOL)Bool{
    [self.navigationController setNavigationBarHidden:Bool animated:NO];
}



#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}


-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SaTMNewPublicPhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SaTMNewPublicPhotoCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    [cell uploadPublicPhotoWithFiles:self.dataSource[indexPath.row] WaterText:self.waterTextStr Type:self.Istype];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _idx = scrollView.contentOffset.x/SCREEN_WIDTH;
    self.title = [NSString stringWithFormat:@"%@/%@",@(_idx + 1),@(self.dataSource.count)];
}

#pragma mark -- 懒加载

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[SaTMNewPublicPhotoCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([SaTMNewPublicPhotoCollectionViewCell class])];
    }
    return _collectionView;
}



@end
